package sunyu.service.shiro.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sunyu.tools.redis.RedisTools;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * shiro使用到的redis配置初始化类
 *
 * @author 孙宇
 */
@Component
public class ShiroRedisConfig {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "redisTools")
    private RedisTools redisTools;

    @Value("${redis.type}")
    private Integer type;//0单机1集群
    @Value("${redis.cluster}")
    private String cluster;
    @Value("${redis.maxTotal}")
    private Integer maxTotal;
    @Value("${redis.maxIdle}")
    private Integer maxIdle;
    @Value("${redis.maxWaitMillis}")
    private Integer maxWaitMillis;
    @Value("${redis.testOnBorrow}")
    private Boolean testOnBorrow;
    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;
    @Value("${redis.connectionTimeout}")
    private Integer connectionTimeout;
    @Value("${redis.password}")
    private String password;

    @PostConstruct
    public void init() {
        logger.info("初始化shiro Redis开始");
        logger.info("redis名称：shiroRedis");
        if (type == 0) {//单机
            logger.info("redis类型：单机");
            logger.info("host：{}", host);
            logger.info("port：{}", port);
            logger.info("password：{}", password);
            redisTools.addJedisPool("shiroRedis", maxTotal, maxIdle, maxWaitMillis, testOnBorrow, connectionTimeout, host, port, password);
        } else if (type == 1) {//集群
            logger.info("redis类型：集群");
            logger.info("cluster：{}", cluster);
            redisTools.addJedisCluster("shiroRedis", maxTotal, maxIdle, maxWaitMillis, testOnBorrow, cluster);
        }
        logger.info("初始化shiro Redis结束");
    }

}
